package java6703.task;
//控制器类
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;

import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Words6703Controller {
    @FXML
    private ListView<String> lvWords;

    @FXML
    private TextArea taWords;

    @FXML
    private Label lblCount;

    @FXML
    void buttonClick(ActionEvent event) {
        //获取ListView并初始化
        ObservableList<String> items = lvWords.getItems();
        items.clear();
        //获取TextArea中输入的文本，转换为小写
        String input = taWords.getText().toLowerCase();
        //使用正则表达式筛选出英文单词
        // String regex = "[a-zA-Z]+";
        String regex = "[a-zA-Z]+";
        System.out.println(input.matches(regex));
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        //将符合要求的英文单词装进TreeSet中
        TreeSet<String> treeSet = new TreeSet<String>();
        while (matcher.find()) {
            treeSet.add(input.substring(matcher.start(), matcher.end()));
        }
        items.addAll(treeSet);
        lblCount.setText("有" + treeSet.size() + "个唯一的单词");
    }
}
